package com.parleys.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.parleys.R;
import com.parleys.domain.PresentationAsset;
import com.parleys.service.intf.PresentationAssetsService;

/**
 * Handles the layout of an element in slides view
 * 
 * @author David Van Droogenbroeck
 * 
 */
public class GalleryImageAdapter extends BaseAdapter {

	private final Context context;
	private final List<PresentationAsset> assets;
	private final String assetBaseUrl;
	private final Integer presentationId;
	private final PresentationAssetsService presentationAssetsService;

	public GalleryImageAdapter(final Context context, final List<PresentationAsset> assets, final String assetBaseUrl,
			final Integer presentationId, final PresentationAssetsService presentationAssetsService) {
		this.context = context;
		this.assets = assets;
		this.assetBaseUrl = assetBaseUrl;
		this.presentationId = presentationId;
		this.presentationAssetsService = presentationAssetsService;
	}

	public int getCount() {
		return assets.size();
	}

	public Object getItem(int position) {
		return assets.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, final View convertView, final ViewGroup parent) {
		ImageView imageView = (ImageView) convertView;
		if (imageView == null) {
			imageView = new ImageView(context);
			imageView.setScaleType(ScaleType.FIT_CENTER);
			imageView.setDrawingCacheEnabled(true);
			imageView.setVerticalFadingEdgeEnabled(false);
			imageView.setHorizontalFadingEdgeEnabled(false);
		}
		
		if (isImageCached(position)) {
			imageView.setImageBitmap(getBitMapForSlideNumber(position));
		} else {
			imageView.setImageResource(R.drawable.icon);
			
			final Handler handler = new Handler();
			final ImageView finalImageView = imageView;
			
			final Runnable imageFetcherRunnable = new Runnable() {
				@Override
				public void run() {
					final Bitmap bitmap = getBitMapForSlideNumber(position);
					
					handler.post(new Runnable() {
						@Override
						public void run() {
							finalImageView.setImageBitmap(bitmap);
						}
					});
				}
			};
			new Thread(imageFetcherRunnable).start();
		}
		return imageView;
	}

	private Bitmap getBitMapForSlideNumber(Integer number) {
		Log.d(GalleryImageAdapter.class.getName(), "getBitMapForSlideNumber: " + number);

		return presentationAssetsService.getImage(assetBaseUrl, "presentations", presentationId, assets.get(number)
				.getValue());
	}
	
	private boolean isImageCached(Integer number) {
		return presentationAssetsService.isImageCached(presentationId, assets.get(number).getValue());
	}

}
